<?php
class ConfigModel {
	public function getList() {
		$sConfigFile = ROOT . '/config.inc.php';
		return parse_ini_file ( $sConfigFile, true );
	}
	
	public function update($aData) {
		if (is_array ( $aData ['key'] )) {
			foreach ( $aData ['key'] as $sGroupName => $aRow ) {
				foreach ( $aRow as $iKey => $sRow ) {
					if ($sRow) {
						$aData ['conf'] [$sGroupName] [$sRow] = $aData ['value'] [$sGroupName] [$iKey];
					}
				}
			}
		}
		$sConfString = '<?php' . "\n";
		foreach ( $aData ['group'] as $sOldGroupName => $sNewGroupNmae ) {
			$sConfString .= '[' . $sNewGroupNmae . "]\n";
			if (is_array ( $aData ['conf'] [$sOldGroupName] )) {
				foreach ( $aData ['conf'] [$sOldGroupName] as $sKey => $sRow ) {
					$sConfString .= $sKey . ' = "' . $sRow . '"' . "\n";
				}
			}
			$sConfString .= "\n";
		}
		copy ( ROOT . '/config.inc.php', ROOT . '/config.inc.php_' . date ( 'Ymd' ) );
		file_put_contents ( ROOT . '/config.inc.php', $sConfString );
	}
}